package com.jokul.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import com.jokul.model.Blah;

/**
 *@author: jokul
 *@2013-3-15 下午4:27:15
 */

public class BlahDaoImpl implements BlahDao {

	private DataSource dataSource;

	public BlahDaoImpl(DataSource dataSource) {
		super();
		this.dataSource = dataSource;
	}

	@Override
	public List<Blah> getBlahs(Blah blah) {
		Connection connection = null;
		PreparedStatement statement = null;
		List<Blah> blahs = null;
		String sql = "select date,txt from blah where name = ?";
		
		try {
			connection = dataSource.getConnection();
			statement = connection.prepareStatement(sql);
			statement.setString(1, blah.getName());
			ResultSet set = statement.executeQuery();
			blahs = new ArrayList<Blah>();
			while(set.next()) {
				blahs.add(new Blah(blah.getName(), set.getTimestamp(1), set.getString(2)));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(statement != null) {
					statement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return blahs;
	}

	@Override
	public int addBlah(Blah blah) {
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = dataSource.getConnection();
			statement = connection.prepareStatement(
					"INSERT INTO blah (NAME,date,txt) VALUES (?,?,?)");
			statement.setString(1, blah.getName());
			statement.setTimestamp(2, new Timestamp(blah.getDate().getTime()));
			statement.setString(3, blah.getTxt());
			return statement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(statement != null) {
					statement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

	@Override
	public int deleteBlah(Blah blah) {
		Connection connection = null;
		PreparedStatement statement = null;
		try {
			connection = dataSource.getConnection();
			statement = connection.prepareStatement(
					"delete from blah where date = ?");
			statement.setTimestamp(1, new Timestamp(blah.getDate().getTime()));
			return statement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				if(statement != null) {
					statement.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			try {
				if(connection != null) {
					connection.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return 0;
	}

}
